
// interface Options {
//     scheduler?: Function
// }
let activeEffect;
export const effect = (fn, options) => {
    const _effect = function() {
        activeEffect = _effect;
        let res = fn()
        return res
    }
    _effect.options = options;
    _effect();
    return _effect
}

// 依赖收集
const targetMap = new WeakMap();
export const track = (target, key) => {
    let depsMap = targetMap.get(target);
    if (!depsMap) {
        depsMap = new Map();
        targetMap.set(target, depsMap);
    }
    let deps = depsMap.get(key);
    if (!deps) {
        deps = new Set();
        depsMap.set(key, deps);
    }
    deps.add(activeEffect);
}

// 触发依赖
export const trigger = (target, key) => {
    const depsMap = targetMap.get(target);
    if (!depsMap) return;
    const deps = depsMap.get(key);
    if (!deps) return;
    deps.forEach(effect => {
        if (effect?.options?.scheduler) {
            effect?.options?.scheduler?.()
        }else {
            effect()
        }
    });
}